package com.wx.disparent.mallparent.controller;

import com.wx.disparent.mallparent.entity.AddressEntity;
import com.wx.disparent.mallparent.entity.OrderEntity;
import com.wx.disparent.mallparent.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 曾右权
 * @description
 * @date 2019/12/11 9:34
 */
@RestController
@RequestMapping("/front/addressPhone")
public class AddressPhoneController {
    @Autowired
    private AddressService addressService;

    /**
     * 跳转到修改地址页面
     * @param id 地址id
     * @return ModelAndView
     */
    @RequestMapping("/addressUpdate")
    public ModelAndView addressUpdate(Integer id, Integer productId, Integer totalNum) {
        ModelAndView model = new ModelAndView();
        System.out.println("************" + id);
        if (id != null && !id.toString().equals("")) {
            AddressEntity addressEntity = addressService.findById(id);
            model.addObject("addressEntity", addressEntity);
            model.addObject("productId", productId);
            model.addObject("totalNum", totalNum);
            model.setViewName("web/addressUpdate");
        } else {
            model.setViewName("web/login");
        }
        return model;
    }

    /**
     * 修改地址
     * @param id 地址id
     * @return 执行结果 0失败
     */
    @RequestMapping("/addressUpdateAndSave")
    public String addressUpdateAndSave(Integer id, String userId, String receiverMobile, String receiverContact, String receiverAddress) {
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setId(id);
        addressEntity.setUserId(userId);
        addressEntity.setReceiverMobile(receiverMobile);
        addressEntity.setReceiverContact(receiverContact);
        addressEntity.setReceiverAddress(receiverAddress);
        System.out.println(addressEntity.getId());
        System.out.println(addressEntity.getReceiverContact());
        return "" + addressService.update(addressEntity);
    }
    /**
     * 删除地址
     * @param id 地址id
     * @return 执行结果 0失败
     */
    @RequestMapping("/addressDelete")
    public ModelAndView addressDelete(String userId,Integer id,Integer productId, Integer totalNum) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/front/addressPhone/findAddressAll?userId="+userId+"&productId="+productId+"&totalNum="+totalNum);
        addressService.deleteById(id);
        return modelAndView;
    }

    /**
     * 跳转到新增地址页面
     * @return ModelAndView
     */
    @RequestMapping("/addressGenerate")
    public ModelAndView addressGenerate(Integer productId, Integer totalNum) {
        System.out.println("*************"+productId+totalNum);
        ModelAndView model = new ModelAndView();
        model.addObject("productId", productId);
        model.addObject("totalNum", totalNum);
        model.setViewName("web/addressGenerate");
        return model;
    }

    /**
     * 新增地址
     * @param addressEntity 地址信息
     * @return 执行结果 0失败
     */
    @RequestMapping("/addressSave")
    public String addressSave(AddressEntity addressEntity) {
        return "" + addressService.insert(addressEntity);
    }


    /**
     * 查询我的地址
     * @param userId    用户id
     * @param productId 商品id
     * @param totalNum  购买数量
     * @return 地址集合
     */
    @RequestMapping("/findAddressAll")
    public ModelAndView findAddressAll(String userId, Integer productId, Integer totalNum) {
        Map<String, Object> paramMap = new HashMap<>();
        ModelAndView modelAndView = new ModelAndView();
//        获取用户id
        if (userId != null && !userId.equals("")) {
            paramMap.put("userId", userId);
            List<AddressEntity> all = addressService.findAll(paramMap);
            modelAndView.addObject("addressList", all);
            modelAndView.addObject("productId", productId);
            modelAndView.addObject("totalNum", totalNum);
            modelAndView.setViewName("web/addressOption");
        } else {
            modelAndView.setViewName("web/login");
        }
        return modelAndView;
    }


    /**
     * 查询我的默认地址
     *
     * @param userId 用户id
     * @return 地址集合
     */
    @RequestMapping("/findDefaultAddress")
    public Map<String, Object> findDefaultAddress(@RequestParam(value = "userId") String userId) {
        System.out.println(userId);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        Map<String, Object> map = new HashMap<String, Object>();
//        获取用户id
        if (userId != null && !userId.equals("")) {
            paramMap.put("userId", userId);
            List<AddressEntity> all = addressService.findAll(paramMap);
            if (all.size() != 0) {
                map.put("message", "true");
                map.put("address", all.get(0));
            } else {
                map.put("message", "false");
            }
        } else {
            map.put("path", "web/login");
        }
        return map;
    }
}
